package cj.web.sc.controllers;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;
import cj.web.constants.SCPropertyTypeEnum;
import cj.web.sc.dao.SCCategoryDAO;
import cj.web.sc.dao.SCPropertyDAO;
import cj.web.sc.domain.SCCategory;
import cj.web.sc.domain.SCProperty;

@Path("/property")
public class SCPropertyController extends BaseController{

	private static Logger logger = Logger.getLogger(SCPropertyController.class.getName());
	
	@Autowired
	private SCPropertyDAO propertyDao;
	
	@Autowired
	private SCCategoryDAO categoryDao;

	@Get("show")
	public String query(Model model,SCProperty property)
	{
		List<Map> categories = categoryDao.queryAll();
		
		model.add("Categories", categories);
		model.add("TypeEnum", EnumSet.allOf(SCPropertyTypeEnum.class));
		model.add("MemuEnum", MemuEnum.scproperty);
		return "/views/sc/propertyList.vm";
	}
	
	@Post("list")
	public String list(Model model,SCProperty property)
	{
		List<Map> objs = propertyDao.queryByCategory(property);
		int size = propertyDao.sizeByCategory(property);
		
		JSONObject jo = new JSONObject();
		jo.put("data", objs);
		jo.put("draw", property.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@" + jo.toString();
	}
	
	@Get("create")
	public String create(Model model)
	{
		model.add("TypeEnum", EnumSet.allOf(SCPropertyTypeEnum.class));
		model.add("MemuEnum", MemuEnum.scproperty);
		return "/views/sc/propertyDetail.vm";
	}
	
	@Post("initCategory")
	public String initCategory(@Param("categoryIds")String ids){
		JSONObject jo = new JSONObject();
		List<Map> rets = new ArrayList<Map>();
		
		List<SCCategory> categories = categoryDao.queryAllObjs();
		for(SCCategory category : categories){
			Map ret = new HashMap<>();
			
			ret.put("id", category.getId());
			ret.put("pId", category.getParentId());
			ret.put("name", category.getName());
			ret.put("open", true);
			
			if(ids != null && ids.length() > 0){
				String[] categoryIds = ids.split(",");
				for( String categoryId :  categoryIds){
					if( Integer.parseInt(categoryId) == category.getId().intValue()){
						ret.put("checked", true);
						break;
					}
					ret.put("checked", false);
				}
			}
			else{
				ret.put("checked", false);
			}
			
			rets.add(ret);
		}
		
		jo.put("ret", rets);
		
		return "@" + jo.toString();
	}
	
	@Post("crtProperty")
	public String crtPropety(Model model,SCProperty property, @Param("categoryIds")String categoryIds, HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		String[] ids = categoryIds.split(",");
		String acount = returnSessionObj(request).getAccount();
		property.setCreateUser(acount);
		property.setUpdateUser(acount);
		property.setDeleteFlag(1);
		
		try {
			for(String categoryId : ids){
				property.setCategoryId(Integer.parseInt(categoryId));
				propertyDao.create(property);
			}
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model, @Param("id") int id, HttpServletRequest request)
	{
		SCProperty obj = propertyDao.queryById(id);
		model.add("obj", obj);
		model.add("categoryIds", obj.getCategoryId());
		model.add("TypeEnum", EnumSet.allOf(SCPropertyTypeEnum.class));
		model.add("MemuEnum", MemuEnum.scproperty);
		return "/views/sc/propertyEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,SCProperty property, HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		String acount = returnSessionObj(request).getAccount();
		property.setUpdateUser(acount);
		
		try {
			propertyDao.update(property);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		return "@" + jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id, HttpServletRequest request)
	{
		SCProperty obj = propertyDao.queryById(id);
		if(obj != null){
			String acount = returnSessionObj(request).getAccount();
			
			obj.setUpdateUser(acount);
			propertyDao.deleteById(obj);
		}
		
		return "r:show";
	}
}
